<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/12/26 0026
 * Time: 14:06
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Tn extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model(array('spider_39/operationt_model' => 'operationt','spider_39/operation_model' => 'operation','spider_39/exam_model' => 'exam'));
    }

    /**
     * 先抓取所有列表
     */
    public function operation($page=1){
        sleep(2);
        $url = 'https://jbk.39.net/bw/t3_p'.$page.'/';
        $content = http_get($url);
        preg_match_all('|<p class="result_item_top_l">(.+?)</p>|is', $content, $match);
        $html = isset($match[1]) ? $match[1] : '';
        if(empty($html)){
            echo "nodata";
            exit;
        }
        if($match[1]){
            foreach($match[1] as $title){
                preg_match('|<a href="(.+?)">(.+?)</a>|is', $title, $titles);
                $url = substr($titles[1],0,strpos($titles[1],'"'));
                $ins = array(
                    'title' => $titles[2],
                    'href' =>$url
                );
                if(!$this->operationt->get_by_where("title='{$titles[2]}' and href='{$url}'")){
                    $this->operationt->insert($ins);
                }

            }
        }
        echo $page+1;
    }
    public function op_content($url = ""){
        if(!$url || $url == 'false'){ // 查找第一条数据
            $info = $this->operationt->get_by_order("order by id asc","*","where status=0");
            if($info){
                $upd['status'] = 1;
                $this->operationt->update($info['id'],$upd);
                $url = $info['href'];
            }else{
                echo "nodata";
            }

        }
        sleep(2);
        $ins = array();
        $content = http_get($url);
        preg_match('|<h1>(.+?)</h1>|is', $content, $titles);
        $ins['operation_name'] = trim(strip_tags($titles[1])); // 标题
        preg_match('|<div class="des" id="intro1">(.+?)</div>|is', $content, $intros);
        $ins['intro'] = str_replace('...[详细][收起]','',trim(strip_tags($intros[1]))); // 介绍
        preg_match('|<dd id="dirList2">(.+?)</dd>|is', $content, $dirs);

        if($dirs){
            preg_match_all('|<span>(.+?)</span>|is', $dirs[1], $lis);
            array_shift($lis[1]);
            $dir = $lis[1]; // 列表信息
            // 分类内容
            preg_match_all('|<article class="w740">(.+?)</article>|is', $content, $navs_info);
            $nav_li = str_replace(' id="refDisease"','',$navs_info[1][0]);
            $navs = explode('<div class="lbox catalogItem">',$nav_li);
            array_shift($navs);
            $navs_filter = array('</div>','text_bd','_change_box_item','&#x3000;',' class=&quot;last&quot;','</a>','">','　',' ',' text_bd">','
');
            foreach($dir as $d){
                $d = strip_tags($d);
                foreach($navs as $n){
                    if(strpos($n,$d) !== false){
                        $n1 = explode('<div class="text',$n);
                        array_shift($n1);
                        $n1 = preg_replace('/<a .*?href="(.*?)".*?>/is',"",$n1);
                        $n1 = implode("",$n1);
                        $n1 = trim(str_replace($navs_filter,'',$n1));
                        if($d == "适应症"){
                            $ins['indication'] = $n1;
                        }
                        if($d == "不适宜人群"){
                            $ins['unsuitable '] = $n1;
                        }
                        if($d == "术前"){
                            $ins['operation_before'] = $n1;
                        }
                        if($d == "手术过程"){
                            preg_match_all('|<ul class="text_art">(.+?)</ul>|is', $n, $n1);
                            $n1 = preg_replace('/<a .*?href="(.*?)".*?>/is',"",$n1[1]);
                            $n1 = str_replace($navs_filter,'',$n1);
                            if($n1){
                                $ins['operation_process'] = "<ul>".$n1[0]."</ul>";
                            }
                        }
                        if($d == "术后"){
                            $ins['operation_after'] = $n1;
                        }
                        if($d == "相关疾病"){
                            preg_match_all('|<ul> (.+?)</ul>|is', $n, $n1);
                            $n1 = preg_replace('/<a .*?href="(.*?)".*?>/is',"",$n1[1]);
                            $n1 = str_replace($navs_filter,'',$n1);
                            if($n1){
                                $ins['relate_disease'] = "<ul>".$n1[0]."</ul>";
                            }
                        }
                        if($d == "相关症状"){
                            preg_match_all('|<ul> (.+?)</ul>|is', $n, $n1);
                            $n1 = preg_replace('/<a .*?href="(.*?)".*?>/is',"",$n1[1]);
                            $n1 = str_replace($navs_filter,'',$n1);
                            if($n1) {
                                $ins['relate_symptom'] = "<ul>".$n1[0]."</ul>";
                            }
                        }
                    }
                }
            }
        }
        // 基本信息
        preg_match_all('|<ul class="infolist">(.+?)</ul>|is', $content, $bases);
        $base = trim($bases[1][0]);
        preg_match_all('|<span>(.+?)</span>|is', $base, $infos);
        if($infos[1]){
            $tags = array('"',' ','\n','\t');
            foreach($infos[1] as $b){
                $b = str_replace($tags,"",strip_tags($b));
                if(strpos($b,"手术部位") !== false){
                    $b = trim(str_replace("手术部位：","",$b));
                    $b = str_replace("
"," ",$b);
                    $ins['positon'] = $b; // 检查部位
                }
                if(strpos($b,"麻醉方式") !== false){
                    $b = trim(str_replace("麻醉方式：","",$b));
                    $b = str_replace("&nbsp;","",$b);
                    $ins['mazui'] = $b; // 麻醉
                }
                if(strpos($b,"手术时间") !== false){
                    $b = trim(str_replace("手术时间：","",$b));
                    $b = str_replace("&nbsp;","",$b);
                    $ins['how_long'] = $b; // 手术时间
                }
                if(strpos($b,"科室") !== false){
                    $b = trim(str_replace("科室：","",$b));
                    $b = str_replace("
"," ",$b);
                    $ins['department'] = $b; // 手术时间
                }
                if(strpos($b,"医院参考价") !== false){
                    $b = trim(str_replace("医院参考价：","",$b));
                    $b = str_replace("查看详细>>","",$b);
                    $ins['fee'] = $b; // 医院参考价
                }
            }
        }
        if(!$this->operation->get_by_where("operation_name='{$ins['operation_name']}'")){
            $this->operation->insert($ins);
        }
        echo "ok";
    }
    public function op_content_no($url = ""){
        if(!$url || $url == 'false'){ // 查找第一条数据
            $info = $this->operation->get_by_order("order by id desc","*","where positon is null");
            if($info){
                $title_info = $this->operationt->get_by_where("title='".$info['operation_name']."'");
                if($title_info){
                    $url = $title_info['href'];
                }

            }else{
                echo "nodata";
            }

        }
        if(!$url){
            echo "nodata";
        }
        //sleep(2);
        $ins = array();
        $content = http_get($url);
//        preg_match('|<h1>(.+?)</h1>|is', $content, $titles);
//        $ins['operation_name'] = trim(strip_tags($titles[1])); // 标题
//        preg_match('|<div class="des" id="intro1">(.+?)</div>|is', $content, $intros);
//        $ins['intro'] = str_replace('...[详细][收起]','',trim(strip_tags($intros[1]))); // 介绍
        preg_match('|<dd id="dirList2">(.+?)</dd>|is', $content, $dirs);

//        if($dirs){
//            preg_match_all('|<span>(.+?)</span>|is', $dirs[1], $lis);
//            array_shift($lis[1]);
//            $dir = $lis[1]; // 列表信息
            // 分类内容
            preg_match_all('|<article class="w740">(.+?)</article>|is', $content, $navs_info);
            $nav_li = str_replace(' id="refDisease"','',$navs_info[1][0]);
            $navs = explode('<div class="lbox catalogItem">',$nav_li);
            array_shift($navs);
            $navs_filter = array('</div>','text_bd','_change_box_item','&#x3000;',' class=&quot;last&quot;','</a>','">','　',' ',' text_bd">','
');
            $dir = array('适应症','不适宜人群','术前','手术过程','术后','相关疾病','相关症状');
            foreach($dir as $d){
                $d = strip_tags($d);
                foreach($navs as $n){
                    if(strpos($n,$d) !== false){
                        $n1 = explode('<div class="text',$n);
                        array_shift($n1);
                        $n1 = preg_replace('/<a .*?href="(.*?)".*?>/is',"",$n1);
                        $n1 = implode("",$n1);
                        $n1 = trim(str_replace($navs_filter,'',$n1));
                        if($d == "适应症"){
                            $ins['indication'] = $n1;
                        }
                        if($d == "不适宜人群"){
                            $ins['unsuitable '] = $n1;
                        }
                        if($d == "术前"){
                            $ins['operation_before'] = $n1;
                        }
                        if($d == "手术过程"){
                            preg_match_all('|<ul class="text_art">(.+?)</ul>|is', $n, $n1);
                            $n1 = preg_replace('/<a .*?href="(.*?)".*?>/is',"",$n1[1]);
                            $n1 = str_replace($navs_filter,'',$n1);
                            if($n1){
                                $ins['operation_process'] = "<ul>".$n1[0]."</ul>";
                            }
                        }
                        if($d == "术后"){
                            $ins['operation_after'] = $n1;
                        }
                        if($d == "相关疾病"){
                            preg_match_all('|<ul> (.+?)</ul>|is', $n, $n1);
                            $n1 = preg_replace('/<a .*?href="(.*?)".*?>/is',"",$n1[1]);
                            $n1 = str_replace($navs_filter,'',$n1);
                            if($n1){
                                $ins['relate_disease'] = "<ul>".$n1[0]."</ul>";
                            }
                        }
                        if($d == "相关症状"){
                            preg_match_all('|<ul> (.+?)</ul>|is', $n, $n1);
                            $n1 = preg_replace('/<a .*?href="(.*?)".*?>/is',"",$n1[1]);
                            $n1 = str_replace($navs_filter,'',$n1);
                            if($n1) {
                                $ins['relate_symptom'] = "<ul>".$n1[0]."</ul>";
                            }
                        }
                    }
                }
            }
//        }
        // 基本信息
        preg_match_all('|<ul id="infofull" class="infolist clearfix">(.+?)</ul>|is', $content, $bases);
        $base = trim($bases[1][0]);
        preg_match_all('|<span>(.+?)</span>|is', $base, $infos);
        if($infos[1]){
            $tags = array('"',' ','\n','\t');
            foreach($infos[1] as $b){
                $b = str_replace($tags,"",strip_tags($b));
                if(strpos($b,"手术部位") !== false){
                    $b = trim(str_replace("手术部位：","",$b));
                    $b = str_replace("
"," ",$b);
                    $ins['positon'] = $b; // 检查部位
                }
                if(strpos($b,"麻醉方式") !== false){
                    $b = trim(str_replace("麻醉方式：","",$b));
                    $b = str_replace("&nbsp;","",$b);
                    $ins['mazui'] = $b; // 麻醉
                }
                if(strpos($b,"手术时间") !== false){
                    $b = trim(str_replace("手术时间：","",$b));
                    $b = str_replace("&nbsp;","",$b);
                    $ins['how_long'] = $b; // 手术时间
                }
                if(strpos($b,"科室") !== false){
                    $b = trim(str_replace("科室：","",$b));
                    $b = str_replace("
"," ",$b);
                    $ins['department'] = $b; // 手术时间
                }
                if(strpos($b,"医院参考价") !== false){
                    $b = trim(str_replace("医院参考价：","",$b));
                    $b = str_replace("查看详细>>","",$b);
                    $ins['fee'] = $b; // 医院参考价
                }
            }
        }
//        if(!$this->operation->get_by_where("operation_name='{$ins['operation_name']}'")){
//            $this->operation->insert($ins);
//        }
        $this->operation->update($info['id'],$ins);
        echo "ok";
    }
    public function change_name(){
        $result = $this->operation->result_all('where operation_name like "%大类手术%"');
        $navs_filter = array('</div>','大类手术','_change_box_item','&#x3000;',' class=&quot;last&quot;','</a>','">','　',' ',' text_bd">','
');
        foreach($result as $k=>$v){
            $name = str_replace($navs_filter,'',$v['operation_name']);
            $up['operation_name'] = $name;
            $this->operation->update($v['id'],$up);
        }

    }
    public function exam_content($url = ""){
        if(!$url || $url == 'false'){ // 查找第一条数据
            $info = $this->operationt->get_by_order("order by id asc","*","where status=0");
            if($info){
                $upd['status'] = 1;
                $this->operationt->update($info['id'],$upd);
                $url = $info['href'];
            }else{
                echo "nodata";
            }

        }
        sleep(2);
        $filter = array('</div>','大类手术','...[详细][收起]','_change_box_item','&#x3000;',' class=&quot;last&quot;','</a>','">','　',' ',' text_bd">','
');
        $ins = array();
        $content = http_get($url);
        preg_match('|<h1>(.+?)</h1>|is', $content, $titles);
        $ins['exam_name'] = trim(str_replace($filter,'',strip_tags($titles[1]))); // 标题
        preg_match('|<div class="des" id="intro">(.+?)</div>|is', $content, $intros);
        $ins['intro'] = trim(str_replace($filter,'',strip_tags($intros[1]))); // 介绍
        $dir = array('不适宜人群','注意事项','包含项目','指标解读结果','相关疾病','相关症状','检查作用','检查过程');

        // 分类内容
        preg_match_all('|<article class="w740">(.+?)</article>|is', $content, $navs_info);
        $nav_li = str_replace(array(' id="refdisease"',' id="refsymptom"'),'',$navs_info[1][0]);
        $navs = explode('<div class="lbox catalogItem">',$nav_li);
        array_shift($navs);
        $navs_filter = array('</div>','text_bd','_change_box_item','&#x3000;',' class=&quot;last&quot;','</a>','　',' text_bd">','
',' class="name"',' class="des"');
        foreach($dir as $d){
            $d = strip_tags($d);
            foreach($navs as $n){
                if(strpos($n,$d) !== false){
                    $n1 = explode('<div class="text">',$n);
                    array_shift($n1);
                    $n1 = preg_replace('/<a .*?href="(.*?)".*?>/is',"",$n1);
                    $n1 = implode("",$n1);
                    $n1 = trim(str_replace($navs_filter,'',$n1));
                    if($d == "不适宜人群"){
                        $ins['unsuitable'] = $n1;
                    }
                    if($d == "注意事项"){
                        $ins['note '] = $n1;
                    }
                    if($d == "包含项目"){
                        preg_match('|<table>(.+?)</table>|is', $n, $n1);
                        $n1 = preg_replace('/<a .*?href="(.*?)".*?>/is',"",$n1[0]);
                        $n1 = str_replace(array('&#x3000;','[详细]','</a>'),'',$n1);
                        if($n1){
                            $ins['include_item'] = $n1;
                        }
                    }
                    if($d == "指标解读结果"){
                        preg_match('|<div class="text text_d">(.+?)</div>|is', $n, $n1);
                        $n1 = preg_replace('/<a .*?href="(.*?)".*?>/is',"",$n1[0]);
                        $n1 = str_replace('</a>',' ',$n1);
                        if($n1){
                            $ins['result_explain'] = $n1;
                        }
                    }
                    if($d == "相关疾病"){
                        $n = str_replace('</ul>
                                                <ul>','',$n);
                        preg_match('|<ul>(.+?)</ul>|is', $n, $n1);
                        //dd($n1);
                        $n1 = preg_replace('/<a .*?href="(.*?)".*?>/is',"",$n1[0]);
                        $n1 = str_replace($navs_filter,'',$n1);
                        if($n1){
                            $ins['relate_disease'] = $n1;
                        }
                    }
                    if($d == "相关症状"){
                        preg_match('|<ul>(.+?)</ul>|is', $n, $n1);
                        $n1 = preg_replace('/<a .*?href="(.*?)".*?>/is',"",$n1[0]);
                        $n1 = str_replace($navs_filter,'',$n1);
                        if($n1) {
                            $ins['relate_symptom'] = $n1;
                        }
                    }
                    if($d == "检查作用"){
                        $ins['effect'] = $n1;
                    }
                    if($d == "检查过程"){
                        $n1 = str_replace('了解更多就诊知识>>','',$n1);
                        $ins['process'] = $n1;
                    }
                }
            }
        }

        // 基本信息
        preg_match_all('|<ul class="infolist">(.+?)</ul>|is', $content, $bases);
        $base = trim($bases[1][0]);
        preg_match_all('|<span>(.+?)</span>|is', $base, $infos);
        $info_filter = array("
","&nbsp;","查看详细>>");
        if($infos[1]){
            $tags = array('"',' ','\n','\t');
            foreach($infos[1] as $b){
                $b = str_replace($tags,"",strip_tags($b));
                if(strpos($b,"检查部位") !== false){
                    $b = trim(str_replace("检查部位：","",$b));
                    $b = str_replace($info_filter," ",$b);
                    $ins['positon'] = $b; // 检查部位
                }

                if(strpos($b,"科室") !== false){
                    $b = trim(str_replace("科室：","",$b));
                    $b = str_replace($info_filter," ",$b);
                    $ins['department'] = $b; // 手术时间
                }
                if(strpos($b,"空腹检查") !== false){
                    $b = trim(str_replace("空腹检查：","",$b));
                    $b = str_replace($info_filter,"",$b);
                    $ins['is_empty'] = $b; // 麻醉
                }
                if(strpos($b,"医院参考价") !== false){
                    $b = trim(str_replace("医院参考价：","",$b));
                    $b = str_replace($info_filter,"",$b);
                    $ins['fee'] = $b; // 医院参考价
                }
            }
        }
        if(!$this->exam->get_by_where("exam_name='{$ins['exam_name']}'")){
            $this->exam->insert($ins);
        }
        echo "ok";
    }
}